package frames;

import controllers.MouseControl;
import labels.CommonLabel;
import utils.CommonUtils;
import utils.PropertiesUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class MainFrame extends JFrame{

    //组件
    private static JFrame main = new JFrame();
    private static JPanel panel;
    private static JButton start;
    private static CommonLabel commonLabel1;
    private static CommonLabel commonLabel2;
    private static CommonLabel commonLabel3;
    private static CommonLabel commonLabel4;
    private static CommonLabel commonLabel5;
    static {
        try {
            commonLabel1 = new CommonLabel(10,0);
            commonLabel2 = new CommonLabel(115,1);
            commonLabel3 = new CommonLabel(220,2);
            commonLabel4 = new CommonLabel(325,3);
            commonLabel5 = new CommonLabel(430,4);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //配置文件
    public static PropertiesUtil settings;
    public static PropertiesUtil dataCache;
    //其他
    public static ArrayList<CommonLabel> labels;

    /**
     * 加载资源
     */
    public static void initialization(){
        //初始化组件
        start = new JButton("生成结果");
        //加载配置文件
        settings = new PropertiesUtil("SETTINGS.properties");
        dataCache = new PropertiesUtil("DATA.properties");
        //其他
        labels = new ArrayList<>();
        labels.add(commonLabel1);
        labels.add(commonLabel2);
        labels.add(commonLabel3);
        labels.add(commonLabel4);
        labels.add(commonLabel5);
    }

    /**
     * 构造方法
     */
    public MainFrame() throws IOException {
        //初始化资源
        initialization();
        //初始化内容窗格
        setPanel();
        //初始化label
        setLabels();
        //初始化按钮
        setBtns();
        //初始化窗口
        setMain();
    }

    /**
     * 初始化panel
     */
    private void setPanel(){
        //使用JPanel来初始化内容窗格，否则不能用方法setOpaque()来使内容窗格透明
        panel = (JPanel) main.getContentPane();
        //设置窗格透明
        panel.setOpaque(false);
        //内容窗格默认的布局管理器为BorderLayout
        panel.setLayout(null);
    }

    /**
     * 初始化label
     */
    public static void setLabels(){
        panel.add(commonLabel1);
        panel.add(commonLabel2);
        panel.add(commonLabel3);
        panel.add(commonLabel4);
        panel.add(commonLabel5);
        for (int i=0;i<5;i++){
            String data = dataCache.get("data"+i);
            if (!data.equals("0")){
                labels.get(i).addEditBtn();
                labels.get(i).addDeleteBtn();
                labels.get(i).text.setText("******");
            }else {
                labels.get(i).addAddBtn();
                break;
            }
        }
    }

    /**
     * 初始化按钮
     */
    private void setBtns(){
        start.setIcon(new ImageIcon("resources/images/label.png"));
        start.setFont(new Font("微软雅黑", Font.PLAIN, 32));
        start.setForeground(Color.WHITE);
        start.setVerticalTextPosition(JLabel.CENTER);
        start.setHorizontalTextPosition(JLabel.CENTER);
        start.setBounds(250,540,300,60);
        //按钮点击处理 lambda表达式
        start.addActionListener((e) -> {
            onButtonStart();
        });
        panel.add(start);
    }

    /**
     * 初始化窗口
     */
    private void setMain() throws IOException {
        //添加鼠标事件
        MouseControl mouseControl = new MouseControl(main);
        main.addMouseListener(mouseControl);
        main.addMouseMotionListener(mouseControl);
        //初始化图形界面
        int width = Integer.parseInt(settings.get("width"));
        int height = Integer.parseInt(settings.get("height"));
        int screenWidth= java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
        main.setTitle(settings.get("title"));
        main.setSize(width, height);
        main.setLocation((screenWidth-width)/2, (screenHeight-width)/2);
        main.setLayout(null);
        main.setBackground(Color.GRAY);
        //设置图标
        BufferedImage image = ImageIO.read(new File("resources/images/icon.png"));
        main.setIconImage(image);
        main.setDefaultCloseOperation(EXIT_ON_CLOSE);
        main.setVisible(true);
        main.repaint();
    }

    //事件处理
    private void onButtonStart() {
        for (int i=0;i<5;i++){
            String data = dataCache.get("data"+i);
            if (!data.equals("0")){
                String result = CommonUtils.createRandomList(data.split(","),1).get(0);
                labels.get(i).text.setText(result);
            }
        }
    }

    /**
     * 刷新窗口
     */
    public static void refreshFrame(){
        main.invalidate();
        main.validate();
        main.repaint();
    }

    /**
     * 删除label
     */
    public static void deleteLabels() {
        commonLabel1.deleteAllBtn();
        commonLabel2.deleteAllBtn();
        commonLabel3.deleteAllBtn();
        commonLabel4.deleteAllBtn();
        commonLabel5.deleteAllBtn();
        panel.remove(commonLabel1);
        panel.remove(commonLabel2);
        panel.remove(commonLabel3);
        panel.remove(commonLabel4);
        panel.remove(commonLabel5);
    }

}
